package util;

import java.math.BigDecimal;
import java.util.Date;

import data.Constant;

public class TimeUtil {
	public TimeUtil() {
		TimeCalc tc = new TimeCalc();
		tc.start();
	}
	private static String today = DateUtil.format(new Date());
	public static BigDecimal proRate = Constant.One;
	public static boolean isDeal = false;
	public static boolean afterDeal = false;
	
	private static void calcTimeProgress(){
		Date amBegin = DateUtil.parseTime(today+" 09:27:00");
		Date amEnd = DateUtil.parseTime(today+" 11:30:00");
		Date pmBegin = DateUtil.parseTime(today+" 13:00:00");
		Date pmEnd = DateUtil.parseTime(today+" 15:00:00");
		Date pmEnd2 = DateUtil.parseTime(today+" 15:15:00");
		Date now = new Date();
		
		if(now.after(amBegin)&&now.before(amEnd)){
			long t = (now.getTime()-amBegin.getTime())/1000;
			proRate=new BigDecimal(t).divide(new BigDecimal(14400),8,BigDecimal.ROUND_HALF_UP);
			isDeal = true;
		}else if(now.after(pmBegin)&&now.before(pmEnd)){
			long t = (now.getTime()-pmBegin.getTime())/1000;
			proRate=new BigDecimal(t).divide(new BigDecimal(14400),8,BigDecimal.ROUND_HALF_UP);
			proRate = proRate.add(new BigDecimal(0.5));
			isDeal = true;
		}else if(now.after(amEnd)&&now.before(pmBegin)){
			proRate = new BigDecimal(0.5);
			isDeal = false;
		}else{
			isDeal = false;
		}
		
		if(now.after(pmEnd2)){
			afterDeal = true;
		}
	}
	
	public static BigDecimal getTimeProgress(){
		return proRate;
	}

	public class TimeCalc extends Thread{
		@Override
		public void run() {
			try {
				while(true){
					Thread.sleep(60000);
					calcTimeProgress();
					if(afterDeal){
						break;
					}
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}
